# -*- coding: utf-8 -*-


import  wx
import  os
import  wx.wizard as wiz
#import  Maketitle
#import  images
import  encodings.mbcs
import  codecs
import GenericTable
import wx.grid
import validator
import excel

import wizdata
from time import gmtime, strftime 

class SimpleGrid(wx.grid.Grid):
    def __init__(self, parent):
        wx.grid.Grid.__init__(self, parent, -1)
        wdata = wizdata.WizData()
        self.filename = wdata.filename
        if self.filename != None:
            self.excel = excel.Excel(self.filename)
            #xls.getData()
            
            data = self.excel.getAllData()
            self.setSession()
            
            colLabels = self.excel.getColLabels()

            rowLabels = self.excel.getRowLabels()

            """
            rowLabels = []
            for x in xrange(len(data)):
                x = "%s"%(x)
                rowLabels.append(x)
            rowLabels = tuple(rowLabels)
            """
            """
            for x in rowLabels:
                print x,
            """
            """
            data
            rowLabels
            colLabels        
        """
        else:
            data = (('','','','1','2','3','1','2','3'),
            ('1','','','1','2','3','1','2','3'),
            ('1','2','3','1','2','3','1','2','3'),
            ('1','2','3','1','2','3','1','2','3'),
            ('1','2','3','1','2','3','1','2','3'),
            ('1','2','3','1','2','3','1','2','3'))
            
            rowLabels = ('0','1','2','3','4','5','6','7','8')
            colLabels = ('0','1','2','3','4','5','6','7','8')
            
            
        self.tableBase = GenericTable.GenericTable(data, rowLabels, colLabels)
        
        self.SetTable(self.tableBase)
        
        #self.SetCellAlignment(0,0,wx.ALIGN_CENTRE,wx.ALIGN_CENTRE)
        #self.SetCellSize(0, 0, 1, 6)
        #self.SetCellAlignment(1,0,wx.ALIGN_CENTRE,wx.ALIGN_CENTRE)
        #self.SetCellSize(1, 0, 1, 6)
    
    def update(self):
        wdata = wizdata.WizData()
        self.excel = excel.Excel(wdata.filename)
        self.setSession()
        
        data = self.excel.getAllData()        
        colLabels = self.excel.getColLabels()
        rowLabels = self.excel.getRowLabels()

        self.tableBase.update(self, data, rowLabels, colLabels)
        self.SetTable(self.tableBase)   ###  how to update when SetTable done
        
        #self.ForceRefresh()
        
    def setSession(self):
        wdata = wizdata.WizData()
        wdata.nrows = self.excel.nrows
        wdata.ncols = self.excel.ncols

        wdata.rowstart = self.excel.rowstart             # row start
        wdata.colstart = self.excel.colstart             # column start
        wdata.exceldata = self.excel.alldata        
        
    def SetData(self):

        xls = excel.Excel()
        xls.getData()
        data = xls.getAllData()
        colLabels = xls.getRowLabel()
        #xls.setData()
        #print type(data)
        rowLabels = []
        for x in xrange(len(data)):
            x = "%s"%(x+1)
            rowLabels.append(x)
        rowLabels = tuple(rowLabels)        
        self.tableBase = GenericTable.GenericTable.SetData(data, rowLabels, colLabels)
        self.SetTable(self.tableBase)
        
class MyGrid:
    def __init__(self,parent):
        grid = wx.grid.Grid(self)
        grid.CreateGrid(5,5)
        for row in range(5):
            for col in range(5):
                grid.SetCellValue(row, col, "(%s,%s)" % (row, col))
        grid.SetCellSize(2, 2, 2, 3)
        
class GridPage(wiz.PyWizardPage):#(wiz.WizardPageSimple):
    def __init__(self, parent, title):
        #wiz.WizardPageSimple.__init__(self, parent)
        wiz.PyWizardPage.__init__(self, parent)
        self.parent = parent
        self.next = self.prev = None
        self.sizer = wx.GridBagSizer(hgap=5, vgap=5)
        #wizPg.SetSizer(sizer)
        title = wx.StaticText(self, -1, title)
        title.SetFont(wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD))
        self.sizer.Add(title, pos = (0,1),span = (1,4))
        
        #self.built = False
        data = wizdata.WizData()
        #data.file = 'salary1.xls'
        self.filename = None #data.file
        self._SetDate()
        self.Build()
        
    def _SetDate(self):
        data = wizdata.WizData()
        now = strftime("%Y-%m-%d", gmtime())
        data.pstart = now
        data.pend = now
        data.inout = now
        
    def Build(self):
        #print "build",
        #print  myfile
        data = wizdata.WizData()
        
        """
        if self.filename == data.filename:
            pass
        else:
            self.filename = data.filename
            if self.built == True:
                grid = SimpleGrid(self)
                grid.SetData()
        """
        #if self.built == False:
        pstart = wx.StaticText(self, -1, u"期间[起始]:", size = (100,-1), style = wx.ALIGN_RIGHT)
        self.sizer.Add(pstart, pos=(1,1))
        self.startid = wx.NewId()
        self.pstartdate = wx.DatePickerCtrl(self, self.startid,  size=(120,-1),
                                style=wx.DP_DROPDOWN | wx.DP_SHOWCENTURY)
        self.Bind(wx.EVT_DATE_CHANGED, self.OnDateChanged, self.pstartdate)
        self.sizer.Add(self.pstartdate, pos=(1,2))

        pend = wx.StaticText(self, -1, u"[截至]:", size = (100,-1), style = wx.ALIGN_RIGHT)
        self.sizer.Add(pend, pos=(1,3))
        
        self.endid = wx.NewId()
        self.penddate = wx.DatePickerCtrl(self, self.endid,  size=(120,-1),
                                style=wx.DP_DROPDOWN | wx.DP_SHOWCENTURY)
        self.Bind(wx.EVT_DATE_CHANGED, self.OnDateChanged, self.penddate)
        self.sizer.Add(self.penddate, pos=(1,4))

        tc = wx.StaticText(self, -1, u"记帐日期:", size = (100,-1), style = wx.ALIGN_RIGHT)
        self.sizer.Add(tc, pos=(1,5))
        
        #periodin= wx.TextCtrl(self, -1, "", validator=validator.CharValidator(), size=(160,-1))
        self.inid = wx.NewId()
        input = wx.DatePickerCtrl(self, self.inid, size=(120,-1),
                                style=wx.DP_DROPDOWN | wx.DP_SHOWCENTURY)
        self.Bind(wx.EVT_DATE_CHANGED, self.OnDateChanged, input)
        self.sizer.Add(input, pos=(1,6))
        
        
        cb = wx.CheckBox(self, -1, u"不使用期间", size = (120,-1))
        self.sizer.Add(cb, pos=(2,6))
        cb.Bind(wx.EVT_CHECKBOX, self.OnChecked)
        
        #mysize = wx.Size(500,200)
        #file ="salary1.xls"
        #if self.filename:
        self.grid = SimpleGrid(self)
        #grid = MyGrid(self)        
        self.sizer.Add(self.grid, pos=(3,1),span=(1,10))
        #self.SetMinSize((500,500))
        self.SetSizer(self.sizer)
        self.Fit()
        #print self.GetClientSize()
        self.built = True
        #
        #self.Bind(wx.EVT_ACTIVATE, self.OnActive)
    def OnChecked(self, event):
        box = event.GetEventObject()
        if box.GetValue():
            self.pstartdate.Enable(False)
            self.penddate.Enable(False)
        else:
            self.pstartdate.Enable(True)
            self.penddate.Enable(True)

        
    def OnDateChanged(self, event):
        
        wdata = wizdata.WizData()
        dpid = event.GetId()
        dp = event.GetEventObject()
        date = dp.GetValue()
        if dpid == self.startid:
            print "start"
            wdata.pstart = date.FormatISODate()
        elif dpid == self.endid:
            print "end"
            wdata.pend = date.FormatISODate()
        elif dpid == self.inid:
            print "in"
            wdata.inout = date.FormatISODate()
        print date.FormatISODate()
        
    def updateView(self):
        self.grid.update()
        """
        (x, y) = self.GetSize()
        x = y = y + 100
        self.parent.SetClientSize((x,y))
        """
  
    def OnActive(self, event):
        #print "here"
        event.Skip()

    def SetNext(self, next):
        self.next = next
      
    def SetPrev(self, prev):
        self.prev = prev
    
    def GetNext(self):
        next = self.next
        return next
        
    def GetPrev(self):
        return self.prev
        
 
class TestFrame(wx.Frame):
    def __init__(self):
        data = wizdata.WizData()
        data.filename = 'F:/dev/excel/0.2/salary2.xls'
        wx.Frame.__init__(self, None, -1, "", size=(800, 660)) 
        wizard = wiz.Wizard(self, -1, u"数据导入向导")
        page1 = GridPage(wizard, u"期间")
        wizard.FitToPage(page1)
        wizard.RunWizard(page1)

def test():
    app = wx.PySimpleApp()
    TestFrame().Show()
    app.MainLoop() 


if __name__ == "__main__":
    test()
